﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;

namespace YJ.Data.MySql
{
    public class ProgramBuilderQuerys : YJ.Data.Interface.IProgramBuilderQuerys
    {
        private DBHelper dbHelper = new DBHelper();
        /// <summary>
        /// 构造函数
        /// </summary>
        public ProgramBuilderQuerys()
        {
        }
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.ProgramBuilderQuerys实体类</param>
        /// <returns>操作所影响的行数</returns>
        public int Add(YJ.Data.Model.ProgramBuilderQuerys model)
        {
            string sql = @"INSERT INTO programbuilderquerys
				(ID,ProgramID,Field,ShowTitle,Operators,ControlName,InputType,Width,Sort,DataSource,DataSourceString,DataLinkID,IsQueryUsers,Value) 
				VALUES(@ID,@ProgramID,@Field,@ShowTitle,@Operators,@ControlName,@InputType,@Width,@Sort,@DataSource,@DataSourceString,@DataLinkID,@IsQueryUsers,@Value)";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@ID", MySqlDbType.VarChar, 36){ Value = model.ID },
				new MySqlParameter("@ProgramID", MySqlDbType.VarChar, 36){ Value = model.ProgramID },
				new MySqlParameter("@Field", MySqlDbType.Text, -1){ Value = model.Field },
				new MySqlParameter("@ShowTitle", MySqlDbType.Text, -1){ Value = model.ShowTitle },
				new MySqlParameter("@Operators", MySqlDbType.VarChar, 50){ Value = model.Operators },
				new MySqlParameter("@ControlName", MySqlDbType.VarChar, 50){ Value = model.ControlName },
				new MySqlParameter("@InputType", MySqlDbType.Int32, 11){ Value = model.InputType },
				model.Width == null ? new MySqlParameter("@Width", MySqlDbType.VarChar, 50) { Value = DBNull.Value } : new MySqlParameter("@Width", MySqlDbType.VarChar, 50) { Value = model.Width },
				new MySqlParameter("@Sort", MySqlDbType.Int32, 11){ Value = model.Sort },
				model.DataSource == null ? new MySqlParameter("@DataSource", MySqlDbType.Int32, 11) { Value = DBNull.Value } : new MySqlParameter("@DataSource", MySqlDbType.Int32, 11) { Value = model.DataSource },
				model.DataSourceString == null ? new MySqlParameter("@DataSourceString", MySqlDbType.LongText, -1) { Value = DBNull.Value } : new MySqlParameter("@DataSourceString", MySqlDbType.LongText, -1) { Value = model.DataSourceString },
				model.DataLinkID == null ? new MySqlParameter("@DataLinkID", MySqlDbType.VarChar, 50) { Value = DBNull.Value } : new MySqlParameter("@DataLinkID", MySqlDbType.VarChar, 50) { Value = model.DataLinkID },
				model.IsQueryUsers == null ? new MySqlParameter("@IsQueryUsers", MySqlDbType.Int32, 11) { Value = DBNull.Value } : new MySqlParameter("@IsQueryUsers", MySqlDbType.Int32, 11) { Value = model.IsQueryUsers },
				model.Value == null ? new MySqlParameter("@Value", MySqlDbType.VarChar, 50) { Value = DBNull.Value } : new MySqlParameter("@Value", MySqlDbType.VarChar, 50) { Value = model.Value }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.ProgramBuilderQuerys实体类</param>
        public int Update(YJ.Data.Model.ProgramBuilderQuerys model)
        {
            string sql = @"UPDATE programbuilderquerys SET 
				ProgramID=@ProgramID,Field=@Field,ShowTitle=@ShowTitle,Operators=@Operators,ControlName=@ControlName,InputType=@InputType,Width=@Width,Sort=@Sort,DataSource=@DataSource,DataSourceString=@DataSourceString,DataLinkID=@DataLinkID,IsQueryUsers=@IsQueryUsers,Value=@Value
				WHERE ID=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@ProgramID", MySqlDbType.VarChar, 36){ Value = model.ProgramID },
				new MySqlParameter("@Field", MySqlDbType.Text, -1){ Value = model.Field },
				new MySqlParameter("@ShowTitle", MySqlDbType.Text, -1){ Value = model.ShowTitle },
				new MySqlParameter("@Operators", MySqlDbType.VarChar, 50){ Value = model.Operators },
				new MySqlParameter("@ControlName", MySqlDbType.VarChar, 50){ Value = model.ControlName },
				new MySqlParameter("@InputType", MySqlDbType.Int32, 11){ Value = model.InputType },
				model.Width == null ? new MySqlParameter("@Width", MySqlDbType.VarChar, 50) { Value = DBNull.Value } : new MySqlParameter("@Width", MySqlDbType.VarChar, 50) { Value = model.Width },
				new MySqlParameter("@Sort", MySqlDbType.Int32, 11){ Value = model.Sort },
				model.DataSource == null ? new MySqlParameter("@DataSource", MySqlDbType.Int32, 11) { Value = DBNull.Value } : new MySqlParameter("@DataSource", MySqlDbType.Int32, 11) { Value = model.DataSource },
				model.DataSourceString == null ? new MySqlParameter("@DataSourceString", MySqlDbType.LongText, -1) { Value = DBNull.Value } : new MySqlParameter("@DataSourceString", MySqlDbType.LongText, -1) { Value = model.DataSourceString },
				model.DataLinkID == null ? new MySqlParameter("@DataLinkID", MySqlDbType.VarChar, 50) { Value = DBNull.Value } : new MySqlParameter("@DataLinkID", MySqlDbType.VarChar, 50) { Value = model.DataLinkID },
				model.IsQueryUsers == null ? new MySqlParameter("@IsQueryUsers", MySqlDbType.Int32, 11) { Value = DBNull.Value } : new MySqlParameter("@IsQueryUsers", MySqlDbType.Int32, 11) { Value = model.IsQueryUsers },
				model.Value == null ? new MySqlParameter("@Value", MySqlDbType.VarChar, 50) { Value = DBNull.Value } : new MySqlParameter("@Value", MySqlDbType.VarChar, 50) { Value = model.Value },
				new MySqlParameter("@ID", MySqlDbType.VarChar, 36){ Value = model.ID }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 删除记录
        /// </summary>
        public int Delete(Guid id)
        {
            string sql = "DELETE FROM programbuilderquerys WHERE ID=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@ID", MySqlDbType.VarChar, 36){ Value = id.ToString() }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 将DataRedar转换为List
        /// </summary>
        private List<YJ.Data.Model.ProgramBuilderQuerys> DataReaderToList(MySqlDataReader dataReader)
        {
            List<YJ.Data.Model.ProgramBuilderQuerys> List = new List<YJ.Data.Model.ProgramBuilderQuerys>();
            YJ.Data.Model.ProgramBuilderQuerys model = null;
            while (dataReader.Read())
            {
                model = new YJ.Data.Model.ProgramBuilderQuerys();
                model.ID = dataReader.GetString(0).ToGuid();
                model.ProgramID = dataReader.GetString(1).ToGuid();
                model.Field = dataReader.GetString(2);
                model.ShowTitle = dataReader.GetString(3);
                model.Operators = dataReader.GetString(4);
                model.ControlName = dataReader.GetString(5);
                model.InputType = dataReader.GetInt32(6);
                if (!dataReader.IsDBNull(7))
                    model.Width = dataReader.GetString(7);
                model.Sort = dataReader.GetInt32(8);
                if (!dataReader.IsDBNull(9))
                    model.DataSource = dataReader.GetInt32(9);
                if (!dataReader.IsDBNull(10))
                    model.DataSourceString = dataReader.GetString(10);
                if (!dataReader.IsDBNull(11))
                    model.DataLinkID = dataReader.GetString(11);
                if (!dataReader.IsDBNull(12))
                    model.IsQueryUsers = dataReader.GetInt32(12);
                if (!dataReader.IsDBNull(13))
                    model.Value = dataReader.GetString(13);
                List.Add(model);
            }
            return List;
        }
        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<YJ.Data.Model.ProgramBuilderQuerys> GetAll()
        {
            string sql = "SELECT * FROM programbuilderquerys";
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql);
            List<YJ.Data.Model.ProgramBuilderQuerys> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
        /// <summary>
        /// 查询记录数
        /// </summary>
        public long GetCount()
        {
            string sql = "SELECT COUNT(*) FROM programbuilderquerys";
            long count;
            return long.TryParse(dbHelper.GetFieldValue(sql), out count) ? count : 0;
        }
        /// <summary>
        /// 根据主键查询一条记录
        /// </summary>
        public YJ.Data.Model.ProgramBuilderQuerys Get(Guid id)
        {
            string sql = "SELECT * FROM programbuilderquerys WHERE ID=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@ID", MySqlDbType.VarChar, 36){ Value = id.ToString() }
			};
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.ProgramBuilderQuerys> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List.Count > 0 ? List[0] : null;
        }

        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<YJ.Data.Model.ProgramBuilderQuerys> GetAll(Guid programID)
        {
            string sql = "SELECT * FROM ProgramBuilderQuerys WHERE ProgramID=@ProgramID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@ProgramID", MySqlDbType.VarChar){ Value = programID.ToString() }
			};
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.ProgramBuilderQuerys> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }

        /// <summary>
        /// 删除记录
        /// </summary>
        public int DeleteByProgramID(Guid id)
        {
            string sql = "DELETE FROM ProgramBuilderQuerys WHERE ProgramID=@ProgramID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@ProgramID", MySqlDbType.VarChar){ Value = id.ToString() }
			};
            return dbHelper.Execute(sql, parameters);
        }

       
    }
}